package com.shimao.celnet.payment.controller.apinotcheck;

import com.baiwang.bop.client.BopException;
import com.baiwang.bop.client.IBopClient;
import com.baiwang.bop.request.impl.invoice.impl.FormatfileBuildRequest;
import com.baiwang.bop.respose.entity.FormatfileQueryResponse;
import com.shimao.celnet.payment.model.PInvoiceBlue;
import com.shimao.celnet.payment.model.PInvoiceRed;
import com.shimao.celnet.payment.service.apiservice.CallOut_invOpenOutService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 世茂支付平台 → 百望云平台；   【数据】：电子发票地址取得；
 */
@Service
public class CallOut_invPdfOutController {
    protected Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private CallOut_invOpenOutService invOpenOutService;

    public String  invPdfOut(PInvoiceBlue pInvoiceBlue) {
        try {
            /**
             * 1、登录授权获取Token
             * 2、组装请求类
             * 3、发送请求获取返回值
             */
            String token = invOpenOutService.getToken();
            FormatfileBuildRequest request = new FormatfileBuildRequest();
            request.setSellerTaxNo(pInvoiceBlue.getSellerTaxNo());//纳税人识别号
            request.setSerialNo(pInvoiceBlue.getSerialNo());//开票流水号
            request.setInvoiceCode(pInvoiceBlue.getInvoiceCode());//发票代码
            request.setInvoiceNo(pInvoiceBlue.getInvoiceNo());//发票号码
            request.setPushType("1");//推送标志 0：不推送  1：推送（邮箱电话必填一个）
            request.setBuyerPhone(pInvoiceBlue.getBuyerPhone());// 购方客户电话
            request.setBuyerEmail(pInvoiceBlue.getBuyerEmail());// 购方客户邮箱
            //同步请求
            IBopClient client =invOpenOutService.getClient();
            FormatfileQueryResponse response = client.execute(request, token, FormatfileQueryResponse.class);
            return response.getData();
        } catch (BopException e) {
            logger.info("版式生成失败");
            logger.info(e.getErrCode());
            logger.info(e.getErrMsg());
            logger.info(e.getSubMessage());
            return  null;
        }

    }

    public String invPdfOut(PInvoiceRed pInvoiceResd) {
        try {
            /**
             * 1、登录授权获取Token
             * 2、组装请求类
             * 3、发送请求获取返回值
             */
            String token = invOpenOutService.getToken();
            FormatfileBuildRequest request = new FormatfileBuildRequest();
            request.setSellerTaxNo(pInvoiceResd.getSellerTaxNo());//纳税人识别号
            request.setSerialNo(pInvoiceResd.getSerialNo());//开票流水号
            request.setInvoiceCode(pInvoiceResd.getInvoiceCode());//发票代码
            request.setInvoiceNo(pInvoiceResd.getInvoiceNo());//发票号码
            request.setPushType("1");//推送标志 0：不推送 1：推送（邮箱电话必填一个）
            request.setBuyerPhone(pInvoiceResd.getBuyerPhone());// 购方客户电话
            request.setBuyerEmail(pInvoiceResd.getBuyerEmail());// 购方客户邮箱

            //同步请求
            IBopClient client = invOpenOutService.getClient();
            FormatfileQueryResponse response = client.execute(request, token, FormatfileQueryResponse.class);

            return response.getData();
        } catch (BopException e) {
            logger.info("版式生成失败");
            logger.info(e.getErrCode());
            logger.info(e.getErrMsg());
            logger.info(e.getSubMessage());
            return null;
        }

    }
}


